<!DOCTYPE html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生报名系统</title>
    <link rel="shortcut icon" href="./img/favicon.ico">

    <link href="./css/bootstrap.min.css" rel="stylesheet">
    <link href="./css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="./css/animate.min.css" rel="stylesheet">
    <link href="./css/style.min.css" rel="stylesheet">
    <link href="./css/common.min.css" rel="stylesheet">
    <link href="./css/plugins/fontawesome/releases/css/all.css" rel="stylesheet">


    <link rel="stylesheet" href="css/plugins/fontawesome/releases/css/all.css">
    <link rel="stylesheet" href="css/bootstrap-table.min.css">

</head>
<body class="gray-bg">
<div class="wrapper wrapper-content" style="padding-top: 30px;">
    <div class="container animated rotateInDownLeft">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h2>
                            <b>社团展示</b>
                            <a data-toggle="modal" class="btn btn-primary" href="#modal-form">加入社团</a>
                        </h2>
                        <div>
                            <table data-toggle="table" id="table" data-pagination="true">
                                <thead>
                                <tr>
                                    <th data-field="id">社团编号</th>
                                    <th data-field="name">社团</th>
                                    <th data-field="details">简介</th>
                                    <th data-field="nums">人数</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="modal-form" class="modal fade" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-12">
                        <h3 class="m-t-none m-b">加入社团</h3>
                        <form id="joinForm">
                            <div class="form-group">
                                <label>学号：</label>
                                <input type="text" placeholder="请输入学号" class="form-control" id="uid" name="uid"
                                       readonly="readonly">
                            </div>
                            <div class="form-group">
                                <label>社团编号：</label>
                                <div>
                                    <select class="form-control" name="oid" id="oid">
                                        <option>--请选择--</option>
                                    </select>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" onclick="join()">确定</button>
                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                    取消
                                </button>
                            </div>
                        </form>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<script src="./js/jquery.min.js" type="text/javascript"></script>
<script src="./js/plugins/ajax/popper.min.js" type="text/javascript"></script>
<script src="./js/bootstrap.min.js" type="text/javascript"></script>
<script src="./js/plugins/bootstrap/bootstrap-table.min.js" type="text/javascript"></script>
<script src="./js/plugins/bootstrap/bootstrap-table-zh-CN.js" type="text/javascript"></script>
<script src="./js/common.js"></script>

<script type="text/javascript">
    /*$(function () {
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/api/organ/queryOrganList",
            dataType: "json",
            success: function (msg) {
                $("#table").bootstrapTable('load', msg.data);
                for (var i = 0; i < msg.data.length; i++) {
                    var option = document.createElement("option");
                    /!*$(option).val("[" + msg.data[i].id + "]" + msg.data[i].name);
                    $(option).text("[" + msg.data[i].id + "]" + msg.data[i].name);*!/
                    $(option).val(msg.data[i].id);
                    $(option).text(msg.data[i].id);
                    $('#oid').append(option);
                }
            },
            error: function () {
                //alert("错误");
            }
        });
    });*/

    $("#table").bootstrapTable({
        url: "http://localhost:8080/api/organ/queryOrganList",
        data_local: "zh-US",//表格汉化
        pagination: true,
        pageNumber: "1",
        pageSize: "10",
        uniqueId: "id",
        striped: true, //是否显示行间隔色
        sidePagination: 'client', //client：前端分页
        pageList: [10, 20], //可选择单页记录数
        search: true,
        showRefresh: true,
        showToggle: true,
        showColumns: true,
        iconsPrefix: 'fa',
        icons: {
            refresh: "fa-sync",
            toggleOff: "fa-toggle-off",
            toggleOn: "fa-toggle-on",
            columns: "fa-th-list",
        },
        responseHandler: function (msg) {
            for (var i = 0; i < msg.data.length; i++) {
                var option = document.createElement("option");
                $(option).val(msg.data[i].id);
                $(option).text(msg.data[i].name);
                $('#oid').append(option);
            }
            return msg.data
        }
    })


    /*$(function () {
        $.ajax({
            type: "GET",
            url: "https://www.fastmock.site/mock/f50b06873b252f82df8edabf4bd868a4/demo/student",
            dataType: "json",
            success: function (data) {
                $("#id").val(data.id);
            }
        })
    });*/

    $(function () {
        var stuId;
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/api/system/currentUser",
            // url: "https://www.fastmock.site/mock/f50b06873b252f82df8edabf4bd868a4/demo/student",
            dataType: "json",
            success: function (data) {
                stuId = data.user;
                $.ajax({
                    type: "GET",
                    url: "http://localhost:8080/api/student/queryStudentListById?id=" + stuId,
                    // url: "https://www.fastmock.site/mock/f50b06873b252f82df8edabf4bd868a4/demo/student",
                    dataType: "json",
                    success: function (data) {
                        $("#uid").val(data.id);
                    }
                });
            }
        });


    });
    /*
        $(function () {
            $.ajax({
                type: "GET",
                url: "http://localhost:8080/api/student/queryStudentListById/201641413417",
                dataType: "json",
                success: function (data) {
                    $("#id").val(data.id);
                }
            })
        });
    */

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }

    function join() {
        if ($("#oid").val() == "--请选择--") {
            alert("请选择社团编号！！")
        } else {
            $.ajax({
                // url: "/student2/shetuan",  // 接口地址
                url: "http://localhost:8080/api/organ/affair/insertOrganAffair",
                // url: "http://localhost:8080/api/organ/user/insertOrganUser",
                type: "POST",
                // data: JSON.stringify($("#joinForm").serializeObject()),//将表单序列化成一个对象，这里多个表单相加
                data: JSON.stringify($.extend({}, $("#joinForm").serializeObject(), {
                    'tips': $('#joinForm').find('option:selected').text(),
                    'status': "1",
                    'priority': "1",
                    "logname": "加入社团"
                })),
                //serializeObject()引入上述函数，将表单序列化成一个对象
                //JSON.stringify()将对象转成字符串
                contentType: "application/json",
                datatype: "json",
                success: function (data) {
                    if(data.code == 0){
                        alert("加入社团成功");
                    }else{
                        alert(data.msg);
                    }
                    // swal(data.message);
                    // location.href="scm_po_fhqd.html";
                    console.log(JSON.stringify($.extend({}, $("#joinForm").serializeObject(), {
                        'tips': $('#joinForm').find( 'option:selected').text(),
                        'status': 1,
                        'priority': 1
                    })));
                }, error: function (XMLHttpRequest) {
                    console.log("错误状态：" + XMLHttpRequest.status);
                }
            })

            // window.location.reload()
        }
    }
</script>

</body>
</html>